<template>
  <div class="app-container">
    <div class="filter-container mb10">
      <el-input v-model="queryParam.department_name" size="small" placeholder="组织名称" clearable class="filter-item form-search-input" @keyup.enter.native="handleSearch" />
      <el-select v-model="queryParam.status" size="small" clearable placeholder="请选择" class="filter-item" style="margin-right: 5px">
        <el-option value="1" label="启用" />
        <el-option value="2" label="禁用" />
      </el-select>
      <el-button size="small" type="primary" class="filter-item search" icon="el-icon-search" @click="handleSearch">
        搜索
      </el-button>
      <el-button size="small" class="filter-item" icon="el-icon-refresh" @click="handleRefresh">
        重置
      </el-button>
      <el-button v-action="'Department@save'" size="small" class="filter-item fr" type="primary" icon="el-icon-plus" @click="handleCreate">
        新增
      </el-button>
    </div>
    <el-table v-loading="loading" :data="data" style="width: 100%;margin-bottom: 20px;" row-key="id" border :tree-props="{children: 'children', hasChildren: 'hasChildren'}" :max-height="screenHeigh">
      <el-table-column prop="department_name" label="组织名称" align="center" />
      <el-table-column prop="sort" label="排序" sortable align="center" />
      <el-table-column prop="status" label="状态" sortable align="center">
        <template slot-scope="department">
          <el-button v-if="department.row.status === 1" size="small">启用</el-button>
          <el-button v-else size="small" type="danger">禁用</el-button>
        </template>
      </el-table-column>
      <el-table-column prop="created_at" label="创建时间" align="center" />
      <el-table-column label="操作">
        <template slot-scope="department">
          <el-button v-action="'Department@save'" size="small" type="primary" icon="el-icon-plus" @click="formFieldsData.parent_id=department.row.id;handleCreate()" />
          <el-button v-action="'Department@update'" size="small" type="primary" icon="el-icon-edit" @click="handleUpdate(department.row)" />
          <el-button v-action="'Department@delete'" size="small" type="danger" icon="el-icon-delete" @click="handleDelete(department.row.id)" />
        </template>
      </el-table-column>
    </el-table>
    <!------------------------- 组织 ------------------------------>
    <el-dialog :title="title" :visible.sync="formVisible" width="30%" @close="handleCancel">
      <el-form :ref="formName" :model="formFieldsData" :rules="rules">
        <el-form-item label="组织名称" :label-width="formLabelWidth" prop="department_name">
          <el-input v-model="formFieldsData.department_name" style="width: 92%" autocomplete="off" clearable />
        </el-form-item>
        <el-form-item label="组织负责人" :label-width="formLabelWidth">
          <el-input v-model="formFieldsData.principal" style="width: 92%" autocomplete="off" clearable />
        </el-form-item>
        <el-form-item label="联系方式" :label-width="formLabelWidth">
          <el-input v-model="formFieldsData.mobile" style="width: 92%" autocomplete="off" clearable />
        </el-form-item>
        <el-form-item label="邮箱" :label-width="formLabelWidth">
          <el-input v-model="formFieldsData.email" style="width: 92%" autocomplete="off" clearable />
        </el-form-item>
        <el-form-item label="状态" :label-width="formLabelWidth">
          <el-radio v-model="formFieldsData.status" :label="1" checked>启用</el-radio>
          <el-radio v-model="formFieldsData.status" :label="2">禁用</el-radio>
        </el-form-item>
        <el-form-item label="排序" :label-width="formLabelWidth">
          <el-input-number v-model="formFieldsData.sort" :min="1" :max="100000" />
        </el-form-item>
        <el-input v-model="formFieldsData.parent_id" type="hidden" />
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="handleCancel">取 消</el-button>
        <el-button type="primary" @click="handleSubmit">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import formOperate from '@/layout/mixin/formOperate'
import cacheList from '@/layout/mixin/cacheList'
import {
  action
} from '@/directive/permission/index.js' // 权限判断指令
export default {
  name: 'SystemDepartment',
  directives: {
    action
  },
  mixins: [formOperate, cacheList],
  data() {
    return {
      otherHeight: 305,
      componentName: 'SystemDepartment',
      formName: 'departments',
      formLabelWidth: '120px',
      formFieldsData: {
        department_name: '',
        principal: '',
        mobile: '',
        email: '',
        status: 1,
        sort: 1,
        parent_id: 0
      },
      queryParam: {
        department_name: '',
        status: ''
      },
      url: 'departments',
      // 验证规则
      rules: {
        department_name: [
          { required: true, message: '请输入组织名称', trigger: 'blur' },
          { min: 1, max: 200, message: '长度在 1 到 200 个字符', trigger: 'blur' }
        ]
      }
    }
  }
}
</script>
